package classes.pintaFormulario
{
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import mx.collections.ArrayCollection;
	import mx.controls.ComboBox;
	
	import spark.components.ComboBox;
	import spark.components.DropDownList;
	import spark.components.List;
	
	public class PosicionadorListas
	{
		public function PosicionadorListas()
		{
		}
		
		public static function changeComboBox(cbx:Object, val:String, keyLabel:String):int {
			
			for (var i: int = 0; i < cbx.length; i++) {
				var className:String = getQualifiedClassName(cbx[i]);
				var refClass:Class = getDefinitionByName(className) as Class;
				var instance:Object = new refClass();
				instance = cbx[i];
				var params:Array = null;
				var id:String = instance[keyLabel];
				
				if ( val == id) {
					return i;
				}
			}
			return -1;  
		}
		
		public static function execMxComboBox(text:String, cb:mx.controls.ComboBox, keyLabel:String):void {
			cb.selectedIndex=changeComboBox(cb.dataProvider, text, keyLabel);
		}
		
		public static function execDropDownList(valorBuscado:String, dropDown:DropDownList, field:String):void {
			dropDown.selectedIndex=changeComboBox(dropDown.dataProvider, valorBuscado, field);
		}
		
		public static function execSparkComboBox(text:String, cb:spark.components.ComboBox, keyLabel:String):void {
			cb.selectedIndex=changeComboBox(cb.dataProvider, text, keyLabel);
		}
		
		public static function execSparkList(valorBuscado:String, list:List, field:String):void {
			list.selectedIndices.push(changeComboBox(list.dataProvider, valorBuscado, field));
			(list.dataProvider as ArrayCollection).refresh();
		}
	}
}